﻿using LCL.Network;
using LCL.Network.Enthernet;
using LCL.Network.Profinet.Siemens;
using LCL.NetworkDemo.Control;
using LCL.NetworkDemo.PLC;
using LCL.WinControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace LCL.NetworkDemo
{
    public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();
        }

        #region Siemens PLC(西门子 PLC)
        private void button1_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormSiemens(SiemensPLCS.S1200));
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormSiemens(SiemensPLCS.S1500));
        }
        private void button3_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormSiemens(SiemensPLCS.S300));
        }
        private void button39_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormSiemens(SiemensPLCS.S400));
        }
        private void button5_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormSiemens(SiemensPLCS.S200Smart));
        }
        private void button10_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormSiemensFW());
        }
        private void button36_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormSiemensPPI());
        }
        #endregion

        #region Melsec PLC(三菱 PLC)
        private void button4_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormMelsecBinary());
        }
        private void button7_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormMelsecAscii());
        }
        private void button13_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormMelsec1EBinary());
        }
        private void button32_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormMelsecSerial());
        }
        private void button38_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormMelsecLinks());
        }
        #endregion

        #region Omron PLC(欧姆龙 PLC)
        private void button11_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormOmron());
        }
        #endregion

        #region Panasonic(松下 PLC)
        private void button35_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormPanasonicMew());
        }
        #endregion

        #region Controls(控件)
        private void button16_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormBasicControl());
        }
        private void button17_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormGauge());
        }
        private void button18_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormCurve());
        }
        private void button20_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormPieChart());
        }
        private void button41_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormPipe());
        }
        #endregion

        #region Function
        private void ShowFrm(Form frm)
        {
            this.Hide();
            System.Threading.Thread.Sleep(200);
            frm.ShowDialog();
            System.Threading.Thread.Sleep(200);
            this.Show();
        }
        private void OpenWebside(string url)
        {
            try
            {
                System.Diagnostics.Process.Start(url);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region Hsl Support(HSL 协议)
      
        private void button29_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormSimplifyNet());
        }
        private void button22_Click(object sender, EventArgs e)
        {//Push Net (消息推送)
            this.ShowFrm(new FormPushNet());
        }
        private void button8_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new FormFileClient()); 
        }
        #endregion

        #region Others(其他)
        private void button31_Click(object sender, EventArgs e)
        {//字节变换
            this.ShowFrm(new FormByteTransfer());
        }
        private void button27_Click(object sender, EventArgs e)
        {
            //注册码功能
            this.ShowFrm(new FormRegister());
        }
        private void button15_Click(object sender, EventArgs e)
        {
            this.ShowFrm(new LCL.LogNet.View.FormLogNetView());
        }
        private void button26_Click(object sender, EventArgs e)
        {
            //订单号测试
        }
        #endregion

        #region 检测到更新
        private void MainFrm_Shown(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ThreadPoolCheckVersion), null);
        }
        private void ThreadPoolCheckVersion(object obj)
        {
            System.Threading.Thread.Sleep(100);
            NetSimplifyClient simplifyClient = new NetSimplifyClient(Utils.GetIP(), 18467);
            Result<NetHandle, string> read = simplifyClient.ReadCustomerFromServer(1, Utils.FrameworkVersion.ToString());
            if (read.Success)
            {
                SystemVersion version = new SystemVersion(read.Data2);
                if (version > Utils.FrameworkVersion)
                {
                    this.Invoke(new Action(() =>
                    {

                        if (MessageBox.Show("服务器有新版本：" + read.Data2 + Environment.NewLine + "是否启动更新？", "检测到更新", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            try
                            {
                                System.Diagnostics.Process.Start(Application.StartupPath + "\\软件自动更新.exe");
                                System.Threading.Thread.Sleep(50);
                                Close();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("更新软件丢失，无法启动更新： " + ex.Message);
                            }
                        }

                    }));
                }
            }
        }
        #endregion

        #region Modbus
        private void button6_Click(object sender, EventArgs e)
        {
            //Modbus Tcp
            this.ShowFrm(new FormModbus());
        }
        private void button19_Click(object sender, EventArgs e)
        {
            //异形 Modbus Tcp FormModbusAlien
            this.ShowFrm(new FormModbusAlien());
         
        }
        private void button21_Click(object sender, EventArgs e)
        {
            //Modbus Rtu FormModbusRtu
            this.ShowFrm(new FormModbusRtu());
        }
        private void button34_Click(object sender, EventArgs e)
        {
            //Modbus Ascii FormModbusAscii
            this.ShowFrm(new FormModbusAscii());
        }
        #endregion


    }
}
